package com.yskj.dngame.kotiledemo.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.yskj.dngame.kotiledemo.R
import com.yskj.dngame.kotiledemo.bean.DataAdapterBean
import com.yskj.dngame.kotiledemo.view.DataView

/**
 * 适配器
 * Created by SYZ
on 2017/8/28.
 */
class DataAdapter(var context: Context, var listData: List<DataAdapterBean>) :
        RecyclerView.Adapter<DataAdapter.ViewHolder>() {

    var dataView: DataView? = null

    /**
     * 重新设置数据
     */
    fun setData(list: List<DataAdapterBean>) {
        this.listData = list
        notifyDataSetChanged()
    }


    /**
     * 获取监听事件
     */
    fun setOnItem(dataViews: DataView) {
        this.dataView = dataViews
    }

    /**
     * 返回单个item
     */
    override fun getItemCount(): Int {
        return listData.size
    }

    /**
     * 初始化item
     */
    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int):
            ViewHolder? = ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_data, parent!!, false))


    /**
     * 给单个item 设置参数 赋值
     */
    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
        //给单个item 赋值
        holder!!.names!!.text = listData[position].name
        //点击事件
        holder!!.itemView.setOnClickListener { v ->
            dataView!!.onClick(position)
            dataView!!.onDatas(listData[position].name)
        }

    }

    /**
     * 给默认view 赋值
     */
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val names: TextView? = itemView.findViewById(R.id.names)
    }

}